* PROJECT: "What Leads Racially Resentful Voters to Choose Black Candidates?" for The Journal of Politics
** TASK: Replicate experimental results

********************************************************************************
* READ ME *
** Please make sure you change the working directory below and that your folder
** has a subfolder entitled "Tables"
********************************************************************************
clear all
set more off


** SET DIRECTORY
*	if c(username)== "YOUR_USERNAME_HERE" {
*		cd "YOUR/DIRECTORY/HERE"
*		global slash /
*	}

*	else {
*	display "SET YOUR WORKING DIRECTORY"
		
*	}
	
********************************************************************************

use "Experiment_final.dta", clear

set scheme Plotplainblind


********************************************************************************
**************************** PAPER TABLE AND FIGURES ***************************
********************************************************************************

*******
* Numbers on page 17 are drawn from the analyses below
*******	
	** Number of respondents who completed the MTURK survey
		fre source if source == 2 & white == 1
		
	** Cronbach's alpha for racial resentment among white respondents who completed the MTURK survey	
		alpha resent1 resent2 resent3 resent4 if source==2 & white==1, i

		
*******
* Numbers on page 18 are drawn from the analyses below
*******	
	** Number of respondents who completed the CCES surveys
		fre source if source == 1 & white == 1
		fre source if source == 0 & white == 1
		
	** Cronbach's alpha for racial resentment among white respondents who completed the MTURK survey	
		alpha resent1 resent2 resent3 resent4 if source==1 & white==1, i
		alpha resent1 resent2 resent3 resent4 if source==0 & white==1, i

		
	** Balance of respondent partisanship among CCES modules
		fre pid7 if white==1 & (source == 0 | source == 1)

	
*******
* FIGURE 2 - Candidate Ideology by Experimental Condition
*******
	** Reorder treatments to make graph cleaner 
		gen exp_treat2=.
		replace exp_treat2=1 if exp_treat==1
		replace exp_treat2=2 if exp_treat==2
		replace exp_treat2=3 if exp_treat==6
		replace exp_treat2=4 if exp_treat==5
		replace exp_treat2=5 if exp_treat==3
		replace exp_treat2=6 if exp_treat==4

		label def exp_treat2 1 "GOP No Message" 2 "GOP Message" 3 "No Party Message" 4 "No Party No Message" 5 "Democrat No Message" 6 "Democrat Message"
		label val exp_treat2 exp_treat2
		label variable exp_treat2 "Racial Resentment Experiment treatment (reordered)"

		reg jw_ideol b4.exp_treat2##c.rrindex i.source if white==1, r
		margins exp_treat2
		marginsplot, recast(scatter)

		
*******
* TABLE 3 - Effect of Racial Resentment and Experimental Conditions on Candidate Traits	
*******
	reg diff_lazy b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
	outreg2 using "Tables/Table 3", dec(2) replace
	reg diff_intel b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
	outreg2 using "Tables/Table 3", dec(2) append
	reg diff_trust b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
	outreg2 using "Tables/Table 3", dec(2) append
	reg diff_strong b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
	outreg2 using "Tables/Table 3", dec(2) append
	reg diff_cares b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
	outreg2 using "Tables/Table 3", dec(2) append tex

	
*******	
* FIGURE 3 - Predicted Effect of Racial Resentment on Candidate Traits by Experimental Condition
*******
	** Trait: Lazy
		reg diff_lazy b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
		margins exp_treat, dydx(rrindex) post coeflegend saving(traits1, replace)
		marginsplot, horizontal xlabel(-3(.25)3) recast(scatter) xline(0) saving(traits1, replace)
	
	** Trait: Intelligent
		reg diff_intel b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
		margins exp_treat, dydx(rrindex) post coeflegend saving(traits2, replace)
		marginsplot, horizontal xlabel(-3(.25)3) recast(scatter) xline(0) saving(traits2, replace)
	
	** Trait: Trustworthy
		reg diff_trust b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
		margins exp_treat, dydx(rrindex) post coeflegend saving(traits3, replace)
		marginsplot, horizontal xlabel(-3(.25)3) recast(scatter) xline(0) saving(traits3, replace)
	
	** Trait: Strong Leader
		reg diff_strong b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
		margins exp_treat, dydx(rrindex) post coeflegend saving(traits4, replace)
		marginsplot, horizontal xlabel(-3(.25)3) recast(scatter) xline(0) saving(traits4, replace)
	
	** Trait: Cares
		reg diff_cares b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
		margins exp_treat, dydx(rrindex) post coeflegend saving(traits5, replace)
		marginsplot, horizontal xlabel(-3(.25)3) recast(scatter) xline(0) saving(traits5, replace)

	** COMBINED color-adjusted Figure 3
		combomarginsplot traits1 traits2 traits3 traits4 traits5, horizontal xlabel(-3(.5)3) recast(scatter) xline(0) labels("Lazy" "Intelligent" "Trustworthy" "Strong Leader" "Cares") by(_filenumber)

		
		
*******
* P-Values in text (and in footnote 15) on page 22 and 23 are drawn from the analyses below
*******	
	** "No Party / Message" effect on judgement of intelligence 
		reg diff_intel b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
		margins exp_treat, dydx(rrindex)	

	** "No Party / Message" effect on judgement of laziness
		reg diff_lazy b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
		margins exp_treat, dydx(rrindex)	

	** Difference between the effect of racial resentment on laziness with and without a message 
		* "No Party / No Message" Baseline
			test _b[rrindex:5.exp_treat]=_b[rrindex:6.exp_treat]

	** Difference between effect of racial resentment on intelligence with and without a message
			reg diff_intel b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
			margins exp_treat, dydx(rrindex) post coeflegend
		* Among Democratic candidates
			test _b[rrindex:3.exp_treat]=_b[rrindex:4.exp_treat]

	
*******
* P-Values in text (and in footnotes 17 and 18) on pages 23 and 24 are drawn from the analyses below
*******	
	** Difference between the effect of racial resentment on strong leadership in the presence of a message and without a message 
		* Focus on Democratic candidates
		reg diff_strong b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
		margins exp_treat, dydx(rrindex) post 
		test _b[rrindex:3.exp_treat]=_b[rrindex:4.exp_treat]

	** Cares about people like me
	** Difference between GOP/Message condition and Democrat/No Message condition
		reg diff_cares b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
		margins exp_treat, dydx(rrindex) post
		test _b[rrindex:2.exp_treat] = _b[rrindex:3.exp_treat]
	
	** Difference-of-means test 
		gen rrindex_hi=0
		replace rrindex_hi=1 if rrindex>.5

		for var diff_trust diff_strong diff_cares: ttest X if white==1 & rrindex_hi==1 & (exp_treat==2|exp_treat==3), by(exp_treat)
		for var diff_trust diff_strong diff_cares: ttest X if white==1 & rrindex_hi==1 & (exp_treat==2|exp_treat==5), by(exp_treat)

		
*******		
* TABLE 4 - Effect of the Conditions and Racial Resentment on Vote for the Black Candidate
*******
	probit vote_jamal b5.exp_treat##c.rrindex i.source if white==1, r
	outreg2 using "Tables/Table 4", replace tex(frag) dec(2) e(r2_p ll) 

	
*******	
* FIGURE 4 - Predicted Effect of Racial Resentment by Experimental Condition (All Studies Combined)
*******
	set scheme s1mono
	probit vote_jamal b5.exp_treat##c.rrindex i.source if white==1, r
	margins exp_treat, at(rrindex=(0(0.10)1))
	marginsplot, bydimension(exp_treat) byopts(colfirst) recast(line) recastci(rarea) xlabel(0(.25)1) ylabel(0(.25)1)

	
*******
* Numbers in text on page 27 are drawn from the analyses below
*******		
	** Correlation between racial resentment and individualism indices 
		pwcorr rrindex indiv_index if white==1, sig
	
	
	
*******
* FIGURE 5 - Effect of Racial Resentment on Probability of Voting for Black Candidate 
*******
	** FIGURE 5a - Republican Respondents
		probit vote_jamal b5.exp_treat##c.rrindex i.source if white==1&gop==1, r
		margins exp_treat, at(rrindex=(0(0.10)1))
		marginsplot, bydimension(exp_treat) byopts(colfirst) recast(line) recastci(rarea) xlabel(0(.25)1) ylabel(0(.25)1)

	** FIGURE 5b - Democratic Respondents
		probit vote_jamal b5.exp_treat##c.rrindex i.source if white==1&dem==1, r
		margins exp_treat, at(rrindex=(0(0.10)1))
		marginsplot, bydimension(exp_treat) byopts(colfirst) recast(line) recastci(rarea) xlabel(0(.25)1) ylabel(0(.25)1)


********************************************************************************
************************** APPENDIX TABLE AND FIGURES **************************
********************************************************************************

*******
* TABLE A1 - Descriptive Statistics for Key Variables (p. 2 of appendix)
*******
	** Descriptive statistics for CCES observational data of Table A1 are in "ObservationalAnalysis_JOP.do"
	
	** A note on income data for the experiemnts: 
	*** People who responded "Prefer not to say" to the income question were
	*** recoded to the modal category. The MTurk data income variable is 
	*** faminc2 because income was measured on a different scale in that dataset.
	
	** 2014 MTURK Experiment
		sum rrindex age male educ faminc2 pid7 gop dem indep ideo7 if white==1 & vote_jamal~=. & source == 2 

	** 2014 CCES Experiment
		sum rrindex age male educ faminc pid7 gop dem indep ideo5 if white==1 & vote_jamal~=. & source == 1

	** 2014 CCES Experiment (with Photos)
		sum rrindex age male educ faminc pid7 gop dem indep ideo5 if white==1 & vote_jamal~=. & source == 0


*******
* FIGURE A1 - Distribution of Racial Resentment by Dataset (p. 4 of appendix)
*******
	** Panels a, b, and c of Figure A1 are in "ObservationalAnalysis_JOP.do"

	** (d) 2014 Mechanical Turk
		kdensity rrindex if white==1 & vote_jamal~=. & source==2, bwidth (.2) xlabel (0(.1)1) normal

	** (e) 2014 CCES Experiment
		kdensity rrindex if white==1 & vote_jamal~=. & source==1, bwidth (.2) xlabel (0(.1)1) normal
	
	** (f) 2014 CCES Experiment (with Photos)
		kdensity rrindex if white==1 & vote_jamal~=. & source==0, bwidth (.2) xlabel (0(.1)1) normal

		
*******
* TABLE A8 - Relationship between Racial Resentment and Respondent Partisanship (p. 14 of appendix)
*******	
	** Mean levels of Racial Resentingment for CCES Common surveys of Table A8 are in "ObservationalAnalysis_JOP.do" 
	
	** Generate a 3-point partyid with leaners
		gen pid3_lean=.
		replace pid3_lean=1 if pid7==1|pid7==2|pid7==3
		replace pid3_lean=2 if pid7==4
		replace pid3_lean=3 if pid7==5|pid7==6|pid7==7
	
	** 2014 MTURK Experiment
		ci means rrindex if white==1 & pid3_lean==1 & source ==2
		ci means rrindex if white==1 & pid3_lean==2 & source ==2
		ci means rrindex if white==1 & pid3_lean==3 & source ==2
		*** Pairwise Correlation
			pwcorr rrindex pid7 if white==1 & source==2, sig

	** 2014 CCES Experiment
		ci means rrindex if white==1 & pid3_lean==1 & source ==1
		ci means rrindex if white==1 & pid3_lean==2 & source ==1
		ci means rrindex if white==1 & pid3_lean==3 & source ==1
		*** Pairwise Correlation
			pwcorr rrindex pid7 if white==1 & source==1, sig

	** 2014 CCES Experiment (with Photos)
		ci means rrindex if white==1 & pid3_lean==1 & source ==0
		ci means rrindex if white==1 & pid3_lean==2 & source ==0
		ci means rrindex if white==1 & pid3_lean==3 & source ==0
		*** Pairwise Correlation
			pwcorr rrindex pid7 if white==1 & source==0, sig
		
*******
* TABLE A9 - Percentage of White Respondents Indicating Jamal Williams Was Black/African-American (p.15 of appendix)
*******
	** Source: MTURK
		tabulate exp_treat if white==1 & source==2, summarize(jamal_black)
	
	** Source: CCES
		tabulate exp_treat if white==1 & source==1, summarize(jamal_black)
	
	** Source: CCES (with Photos)
		tabulate exp_treat if white==1 & source==0, summarize(jamal_black)

		
*******
* TABLE A10 - Percentage of White Respondents Indicating Brad Anderson Was White (p.16 of appendix)
*******
	** Source: MTURK
		tabulate exp_treat if white==1 & source==2, summarize(brad_white)

	** Source: CCES
		tabulate exp_treat if white==1 & source==1, summarize(brad_white)
	
	** Source: CCES (with Photos)
		tabulate exp_treat if white==1 & source==0, summarize(brad_white)

		
*******
* FIGURE A6 - Predicted Effect of Racial Resentment on Candidate Traits by Experimental Condition (p.17 of appendix)
*******
	
	set scheme Plotplainblind

	reg diff_lazy b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
	margins exp_treat if (exp_treat==1|exp_treat==2|exp_treat==3|exp_treat==5), at(rrindex=(0(.1)1))
	marginsplot, ylabel(-1(.25)1) recast(line) noci

	reg diff_intel b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
	margins exp_treat if (exp_treat==1|exp_treat==2|exp_treat==3|exp_treat==5), at(rrindex=(0(.1)1))
	marginsplot, ylabel(-1(.25)1) recast(line) noci

	reg diff_trust b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
	margins exp_treat if (exp_treat==1|exp_treat==2|exp_treat==3|exp_treat==5), at(rrindex=(0(.1)1))
	marginsplot, ylabel(-1(.25)1) recast(line) noci

	reg diff_strong b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
	margins exp_treat if (exp_treat==1|exp_treat==2|exp_treat==3|exp_treat==5), at(rrindex=(0(.1)1))
	marginsplot, ylabel(-1(.25)1) recast(line) noci

	reg diff_cares b5.exp_treat##c.rrindex b4.pid7 i.source if white==1, r
	margins exp_treat if (exp_treat==1|exp_treat==2|exp_treat==3|exp_treat==5), at(rrindex=(0(.1)1))
	marginsplot, ylabel(-1(.25)1) recast(line) noci

	grc1leg "Trait_Pred_Trust.gph" "Trait_Pred_Strong.gph" "Trait_Pred_Cares.gph", position(5) ring(0)


**Create color-adjusted figure
**Change directory to location where sub-figures (above) were saved
grc1leg "Trait_Pred_Lazy_jop.gph" "Trait_Pred_Intel_jop.gph" "Trait_Pred_Trust_jop.gph" "Trait_Pred_Strong_jop.gph" "Trait_Pred_Cares_jop.gph", holes(3) position(3) ring(0)



*******
* TABLE A11 - Main Effects of Experimental Treatments (p.18 of appendix)
*******
	** Source: MTURK
		tabulate message_treat party_treat if white==1 & source==2, summarize(vote_jamal) 
		*** Effect of Message Treatment
			* Party Treatment: GOP
				ttest vote_jamal if party_treat==1 & white==1 & source==2, by(message_treat)
			* Party Treatment: Democrat
				ttest vote_jamal if party_treat==3 & white==1 & source==2, by(message_treat)
			* Party Treatment: No Party
				ttest vote_jamal if party_treat==2 & white==1 & source==2, by(message_treat)
			* Overall
				ttest vote_jamal if white==1 & source==2, by(message_treat)

	** Source: CCES
		tabulate message_treat party_treat if white==1 & source==1, summarize(vote_jamal) 
		*** Effect of Message Treatment
			* Party Treatment: GOP
				ttest vote_jamal if party_treat==1 & white==1 & source==1, by(message_treat)
			* Party Treatment: Democrat	
				ttest vote_jamal if party_treat==3 & white==1 & source==1, by(message_treat)
			* Party Treatment: No Party
				ttest vote_jamal if party_treat==2 & white==1 & source==1, by(message_treat)
			* Overall
				ttest vote_jamal if white==1 & source==1, by(message_treat)

	** Source: CCES (with Photos) 
		tabulate message_treat party_treat if white==1 & source==0, summarize(vote_jamal) 
		*** Effect of Message Treatment
			* Party Treatment: GOP
				ttest vote_jamal if party_treat==1 & white==1 & source==0, by(message_treat)
			* Party Treatment: Democrat	
				ttest vote_jamal if party_treat==3 & white==1 & source==0, by(message_treat)
			* Party Treatment: No Party
				ttest vote_jamal if party_treat==2 & white==1 & source==0, by(message_treat)
			* Overall
				ttest vote_jamal if white==1 & source==0, by(message_treat)

				
*******
* TABLE A12 - Average Marginal Effect of Racial Resentment by Condition and Study (p.19 of appendix)
*******
	** Source: MTURK
		*** By condition
			probit vote_jamal b2.party_treat##i.message_treat##c.rrindex if white==1 & source==2, r
			margins message_treat, at(party_treat=(1 2 3)) dydx(rrindex)
		*** Overall
			probit vote_jamal b2.party_treat##i.message_treat##c.rrindex if white==1 & source==2, r
			margins message_treat, dydx(rrindex)
			margins party_treat, dydx(rrindex)
	
	** Source: CCES
		*** By condition
			probit vote_jamal b2.party_treat##i.message_treat##c.rrindex if white==1 & source==1, r
			margins message_treat, at(party_treat=(1 2 3)) dydx(rrindex)
		*** Overall
			probit vote_jamal b2.party_treat##i.message_treat##c.rrindex if white==1 & source==1, r
			margins message_treat, dydx(rrindex)
			margins party_treat, dydx(rrindex)

			
	** Source: CCES (with Pictures)
		*** By condition
			probit vote_jamal b2.party_treat##i.message_treat##c.rrindex if white==1 & source==0, r
			margins message_treat, at(party_treat=(1 2 3)) dydx(rrindex)
		*** Overall
			probit vote_jamal b2.party_treat##i.message_treat##c.rrindex if white==1 & source==0, r
			margins message_treat, dydx(rrindex)
			margins party_treat, dydx(rrindex)


*******
* TABLE A13 - Effect of the Conditions and Racial Resentment on Vote for the Black Candidate by Study (p.20 of appendix)
*******
	** Source: MTURK
		probit vote_jamal b5.exp_treat##c.rrindex if white==1 & source==2, r

	** Source: CCES
		probit vote_jamal b5.exp_treat##c.rrindex if white==1 & source==1, r

	** Source: CCES (with Photos)
		probit vote_jamal b5.exp_treat##c.rrindex if white==1 & source==0, r

		
*******
* FIGURE A7 - Effect of Racial Resentment by Experimental Condition and Study (p.21 of appendix)
*******
	set scheme s1mono
	
	** FIGURE A7(a) - MTURK
		probit vote_jamal b5.exp_treat##c.rrindex if white==1 & source==2, r
		margins exp_treat, at(rrindex=(0(0.10)1))
		marginsplot, bydimension(exp_treat) byopts(colfirst) recast(line) recastci(rarea) xlabel(0(.25)1) ylabel(0(.25)1)

	** FIGURE A7(b) - CCES
		probit vote_jamal b5.exp_treat##c.rrindex if white==1 & source==1, r
		margins exp_treat, at(rrindex=(0(0.10)1))
		marginsplot, bydimension(exp_treat) byopts(colfirst) recast(line) recastci(rarea) xlabel(0(.25)1) ylabel(0(.25)1)

	** FIGURE A7(c) - CCES (with Photos)
		probit vote_jamal b5.exp_treat##c.rrindex if white==1 & source==0, r
		margins exp_treat, at(rrindex=(0(0.10)1))
		marginsplot, bydimension(exp_treat) byopts(colfirst) recast(line) recastci(rarea) xlabel(0(.25)1) ylabel(0(.25)1)


*******
* TABLE A14 - Experimental Results with Demographic Controls (p.22 of appendix)
*******
	** Source: MTURK
		probit vote_jamal b5.exp_treat##c.rrindex b4.pid7 age male educ if white==1 & source==2, r

	** Source: CCES
		probit vote_jamal b5.exp_treat##c.rrindex b4.pid7 age male educ if white==1 & source==1, r

	** Source: CCES (with Photos)
		probit vote_jamal b5.exp_treat##c.rrindex b4.pid7 age male educ if white==1 & source==0, r


*******
* FIGURE A8 - Effect of Racial Resentment by Condition with Controls for Individualism (CCES Module with Pictures) (p.23 of appendix)
*******
	probit vote_jamal b5.exp_treat##c.rrindex b5.exp_treat##c.indiv_index if white==1 & source==0, r
	probit vote_jamal b5.exp_treat##c.rrindex b5.exp_treat##c.indiv_index if white==1, r
	margins exp_treat, at(rrindex=(0(0.10)1))
	marginsplot, bydimension(exp_treat) byopts(colfirst) recast(line) recastci(rarea) xlabel(0(.25)1) ylabel(0(.25)1)

	
*******
* TABLE A15 - Experimental Results with Controls for Individualism (CCES Module with Pictures) (p.24 of appendix)
*******
	probit vote_jamal b5.exp_treat##c.rrindex indiv_index i.source if white==1, r
	outreg2 using "Tables/Table A15", replace dec(2) e(r2_p ll) 
	probit vote_jamal b5.exp_treat##c.rrindex b5.exp_treat##c.indiv_index i.source if white==1, r
	outreg2 using "Tables/Table A15", append tex(frag) dec(2) e(r2_p ll) 
	
	probit vote_jamal b5.exp_treat##c.rrindex b5.exp_treat##c.indiv_index i.source if white==1, r
	margins exp_treat, dydx(rrindex)
	margins exp_treat, dydx(indiv_index)

	
*******
* TABLE A16 - Experimental Results by Respondent Partisanship (p.25 of appendix)
*******
	probit vote_jamal b5.exp_treat##c.rrindex i.source if white==1&gop==1, r
	outreg2 using "Tables/Table A16", replace dec(2) e(r2_p ll) 
	probit vote_jamal b5.exp_treat##c.rrindex i.source if white==1&dem==1, r
	outreg2 using "Tables/Table A16", append dec(2) e(r2_p ll) 
	probit vote_jamal b5.exp_treat##c.rrindex i.source if white==1&indep==1, r
	outreg2 using "Tables/Table A16", append tex(frag) dec(2) e(r2_p ll) 




	



		

